package com.meixiang.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 标签布局(例：刮痧、按摩、补水)
 */
public class LabelLayout extends LinearLayoutCompat {

    private List<String>labels=new ArrayList<>();
    private Context mContext;
    private int resId;
    public LabelLayout(Context context) {
        this(context,null);
    }

    public LabelLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LabelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        //虚拟数据
        labels.add("刮痧");
        labels.add("按摩");
        labels.add("补水哦");
        initView();
    }
    private void initView(){
        resId= R.drawable.shape_circular_green_bg;
        setData();
    }

    /**
     * 设置显示的内容数据
     * @param labels
     */
    public void setLabels(List<String> labels){
        this.labels.clear();
        this.labels.addAll(labels);
        setData();
    }
    public void setLabelImg(int resId){
        this.resId=resId;
        setData();
    }


    private void setData(){
        removeAllViews();
        for (int i=0;i<labels.size();i++){
            TextView tv=new TextView(mContext);
            LayoutParams params=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,DensityUtil.dip2px(mContext,20));
            if (i>0){
                params.leftMargin= DensityUtil.dip2px(mContext,10);
            }
            tv.setLayoutParams(params);
            tv.setPadding(DensityUtil.dip2px(mContext,10),0,DensityUtil.dip2px(mContext,10),0);
            tv.setText(labels.get(i));
            tv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            tv.setGravity(Gravity.CENTER);
            tv.setBackgroundResource(resId);
            addView(tv);
        }
    }
}
